<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ include file="/view/include/css-include.jsp"%>
<%@ include file="/view/include/jsp-include.jsp"%>
<%@ include file="/view/include/jquery-include.jsp"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>

<script type="text/javascript">
    $().ready(function() {    
        //view(1);
        /* 조회버튼 */
        $("#btnSearch").bind("click",function(){     
        	var sGoodsCode      = $("[name=sGoodsCode]").val();
            if(sGoodsCode==null      || sGoodsCode=="") { alert("Product Name Required!!"); $("[name=sGoodsCode]").focus(); return false; }
            view(1);
        });

        $("#cancelBtn").bind("click",function(){       
            self.close();
        });  

        $("#confirmBtn").bind("click",function(){   
            /*var goodsCode = $("#goodsCodeTmp").val();
            var goodsName = $("#goodsNameTmp").val();
            var inDate = $("#inDateTmp").val();
            var mawb = $("#mawbTmp").val();
            var hawb = $("#hawbTmp").val();
            var batchNo = $("#batchNoTmp").val();
            var stockQty = $("#stockQtySum").val();
            var outQty = $("#outQty").val();
            
            if(goodsCode==null   || goodsCode=="") {   alert("Row selected!!"); return false; }*/
           //alert($("#stockOutPopDetailHidden").html());
            var stockQtySum = $("#stockQtySum").val();
            var outQty = $("#outQty").val();
            var outDetQty = "";
            if(parseInt(outQty) > parseInt(stockQtySum)) {   alert("stockQty not enough !!"); return false; }
            //opener.stockOutPopValue(goodsCode, goodsName, inDate, mawb, hawb, batchNo, stockQty, outQty);
            var contents = "";
	        var valCnt = 0;
	        $("#stockOutPopDetailHidden tr").remove();
	        $("[name=checkbox]").each(function(){
	            if($(this).val() == "true"){
	            	var stockQtyTmp = $("[name=stockQtyTmp]").eq(valCnt).html();
	            	if(parseInt(outQty) > 0){
		            	if(parseInt(outQty) >= parseInt(stockQtyTmp)){
		            		outDetQty = stockQtyTmp;
		            	}else{
		            		outDetQty = outQty;
		            		
		            	}
		            	valCnt = $("[name=checkbox]").index(this);                   
	                    contents += "<tr>"
	                    +"<td><input name='goodsCodeDetail' id='goodsCodeDetail' value='"+$("[name=goodsCodeTmp]").eq(valCnt).html()+"'><input name='rowDetailId' id='rowDetailId' value='${rowId}'></td>"
	                    +"<td><input name='goodsNameDetail' id='goodsNameDetail' value='"+$("[name=goodsNameTmp]").eq(valCnt).html()+"'></td>"
	                    +"<td><input name='inDate' id='inDate' value='"+$("[name=inDateTmp]").eq(valCnt).html()+"'></td>"
	                    +"<td><input name='mawbDetail' id='mawbDetail' value='"+$("[name=mawbTmp]").eq(valCnt).html()+"'></td>"
	                    +"<td><input name='hawbDetail' id='hawbDetail' value='"+$("[name=hawbTmp]").eq(valCnt).html()+"'></td>"
	                    +"<td><input name='batchNoDetail' id='batchNoDetail' value='"+$("[name=batchNoTmp]").eq(valCnt).html()+"'></td>"
	                    +"<td><input name='stockQty' id='stockQty' value='"+outDetQty+"'>"
	                    +"<input name='outDateDetail' id='outDateDetail'>"
	                    +"<input name='vihicleNoDetail' id='vihicleNoDetail'>"
	                    +"<input name='outEndUserDetail' id='outEndUserDetail'></td>"
	                    +"</tr>";
	                    $("#batchNo").val($("[name=batchNoTmp]").eq(valCnt).html());
	                    $("#goodsName").val($("[name=goodsNameTmp]").eq(valCnt).html());
	            	}
	            	outQty = parseInt(outQty) - parseInt(stockQtyTmp);
	            }       
	        });
	        $("#stockOutPopDetailHidden").append(contents);
	        if(!confirm("Save?")){
                return false;
             }
	        var goodsName = $("#goodsName").val();
            var batchNo = $("#batchNo").val();
            var outQty2 = $("#outQty").val();
            if(outQty2==null   || outQty2=="") {   alert("OUT QTY required!!"); return false; }
            if(goodsName==null   || goodsName=="") { alert("Row selected!!"); return false; }
            opener.stockOutPopValue($("#stockOutPopDetailHidden").html(), batchNo, outQty2, goodsName);
            self.close();
        });
        
    });

    function view(page){
        $.post(
                '/getStockOutPopUpView.json', {                      
                paging : page,//odrNo,
                listScale : "5",
                blNo : $("#blNo").val(),
                lotNo : $("#lotNo").val(),
                goodsCode : $("#sGoodsCode").val(),
                goodsName : $("#sGoodsName").val(),
                sCustCode : $("#sCustCode", opener.document).val()
               },
            function(data) {
                //$("#productDetail").remove();
                //reSetValidete(); //벨류데이션 초기화
                searchDataMaping(data);    //DATA맵핑 
            });
    }
    
    /* searchData 맵핑 */
    function searchDataMaping(data){
           
        var contents = "";
        var cHeader = "<tr>"
        	+"<th width='40px'>Chk</th>"
        	+"<th width='50px'>Code</th>"
        	+"<th>Name</th>"
        	+"<th width='60px'>In Date</th>"
        	+"<th width='60px'>MAWB</th>"
        	+"<th width='60px'>HAWB</th>"
        	+"<th width='60px'>Batch</th>"
        	+"<th width='70px'>Stock Qty</th>"
        	+"</tr>";
        if(data.resultList.length == 0){
            contents = "<tr><td colspan='8' height='50' bgcolor='#FFFFFF' align='center'><B>No Data</B></td></tr>";
            $("#stockOutPopDetail").html(cHeader+contents);
                
        }else{ 	    
	        $.each(data.resultList, function(index){  // List로 받아온값 (여러건)  
	           contents += "<tr onclick='rowSelect(this);'>"
	               +"<td align='center'><input type='checkbox' name='chk' id='chk' onclick='chkQty("+index+", "+this.stockQty+");'>"
	               +"<input type='hidden' name='checkbox' id='checkbox' value='false'></td>"
	               +"<td align='center'><span name='goodsCodeTmp'>"+this.goodsCode+"</span></td>"
	               +"<td align='center'><span name='goodsNameTmp'>"+this.goodsName+"</span></td>"
	               +"<td align='center'><span name='inDateTmp'>"+this.inDate+"</span></td>"
	               +"<td align='center'><span name='mawbTmp'>"+this.mawb+"</span></td>"
	               +"<td align='center'><span name='hawbTmp'>"+this.hawb+"</span></td>"
	               +"<td align='center'><span name='batchNoTmp'>"+this.batchNo+"</span></td>"
	               +"<td align='right'><span name='stockQtyTmp'>"+this.stockQty+"</span></td>"
	               +"</tr>";
	        });
        }
        var totalPage = Math.ceil(parseInt(data.stockOutCount)/parseInt(5));        
        $("#spanCount").empty().html("Total : "+data.stockOutCount);
        $("#pageCount").empty().html("Page : "+$("#pageNum").val()+" / "+totalPage);                    
        $("#page").empty().html(data.pageSplit);
        
        $("#stockOutPopDetail").html(cHeader+contents);  // 조회 값 뿌리기
        
    }   

    function ajaxPageSplit(num) {
        $("#pageNum").val(num);
        view(num);
    }
    
    function chkQty(idx, qty){
    	var qtySum = $("#stockQtySum").val();
    	var qtyTmp = 0;    	
    	
    	if($("[name=chk]").eq(idx).is(":checked")){
            $("[name=checkbox]").eq(idx).val("true");  
            qtyTmp = parseInt(qtySum) + parseInt(qty);
        }else{
            $("[name=checkbox]").eq(idx).val("false");
            qtyTmp = parseInt(qtySum) - parseInt(qty);
        }
    	$("#stockQtySum").val(qtyTmp);
    }
    

    function rowSelect(obj){
        /*$("#goodsCodeTmp").val(goodsCode);
        $("#goodsNameTmp").val(goodsName);
        $("#inDateTmp").val(inDate);
        $("#mawbTmp").val(mawb);
        $("#hawbTmp").val(hawb);
        $("#batchNoTmp").val(batchNo);
        $("#stockQtyTmp").val(stockQty);*/
        
        $("#stockOutPopDetail tr").css("backgroundColor","#FFFFFF");
        obj.style.backgroundColor="#e0eeee";        
    }
    
    /************************************************************************/
    /*                      상품 팝업 버튼 , 리턴값처리                     */
    /************************************************************************/
    /* 상품 팝업 버튼 */
    function onProductPopup(obj){
        
        window.open('/productPop.html', 'changeProductPop', 'width=615, height=450, toolbar=no, location=no, status=no, menubar=no, scrollbars=no');
    }

    function productPopValue(goodsCode, goodsName, unit, boxQty, hsCode, productCode, custCode, custName){

        $("#sGoodsCode").val(goodsCode);
        $("#sGoodsName").val(goodsName);   
    }
</script>
</head>
<body>
<div class="wrapP2">
    <div class="pagetitle">
        <h1>Product</h1>
    </div>
    <div>
        <table class="TableST">
            <tr>
                <th>BL No</th>
                <td>
                    <input type="text" class="inST" name="blNo" id="blNo"/>
                </td>
                <th>Batch</th>
                <td>
                    <input type="text" class="inST" name="lotNo" id="lotNo"/>
                </td>
            </tr>
            <tr>
                <th>Product Name</th>
                <td colspan="3">
                    <input type="text" class="inST" name="sGoodsName" id="sGoodsName"/>
                    <img src='/images/web/bkn_zoom.gif' aligh='right' id='stockOutPop' onclick='onProductPopup(this);'>
                    <input type="hidden" class="inST" name="sGoodsCode" id="sGoodsCode"/>
                </td>
            </tr>
        </table>
        <div class="bo_btnS">
            <ul class="btnst"><li class="a"><a href="#" id="btnSearch" >SEARCH</a></li></ul>
        </div>
        <br/><br/><br/><br/>
        <ul>
            <li class="totaltext"><span id="spanCount">Total : 0</span></li>
            <li class="pagetext"><span id="pageCount">Page : 1&frasl;1</span></li>
            <li class="bolistW">
                <table class="TableST4" id="stockOutPopDetail">
                    <tr>
                        <th width="40px">CHK</th>
                        <th width="50px">CODE</th>
                        <th>NAME</th>
                        <th width="60px">IN DATE</th>
                        <th width="60px">MAWB</th>
                        <th width="60px">HAWB</th>
                        <th width="60px">LOT.NO</th>
                        <th width="70px">STOCK QTY</th>
                    </tr>
                    <!-- span id="stockOutPopDetail">
                        <tr>
                           <td colspan="10"></td>    
                        </tr>
                    </span-->                  
                </table>
                <div id="viewDialog" style="display: none; background: white;">
                <table class="TableST4">
                    <tr>
                        <th width="50px">CODE</th>
                        <th>NAME</th>
                        <th width="60px">IN DATE</th>
                        <th width="60px">MAWB</th>
                        <th width="60px">HAWB</th>
                        <th width="60px">LOT.NO</th>
                        <th width="70px">STOCK QTY</th>
                    </tr>
                    <span id="stockOutPopDetailHidden">
                        <tr>
                           <td colspan="10"></td>    
                        </tr>
                    </span>                    
                </table>
                </div>
            </li>
        </ul>
    </div>
    <!--<div class="padeNum">
    <a href="#"><img src="images/web/icon_navi_01.gif" /></a>1.2.3.4.5.6.7<a href="#"><img src="images/web/icon_navi_02.gif" /></a>
    </div>
    -->
    <div class="page_wrap">
        <div class="page" id="page" >
        </div>
    </div>
    <br/>
    <table class="TableST5">
        <tr>
            <th>Stock Qty Sum</th>
            <td>
                <input type="text" class="inST" name="stockQtySum" id="stockQtySum" value="0"/>
            </td>
            <th>Out Qty</th>
            <td>
                <input type="text" class="inST" name="outQty" id="outQty"/>
                <input type="hidden" name="batchNo" id="batchNo"/>
                <input type="hidden" name="goodsName" id="goodsName"/>
            </td>
        </tr>
    </table>
    <div class="bo_btn">
        <ul class="btnst"><li class="a"><a href="#" id="confirmBtn" >SAVE</a></li></ul>
        <ul class="btnst"><li class="a"><a href="#" id="cancelBtn" >CANCEL</a></li></ul>
    </div>
    <input type="hidden" name="pageNum" id="pageNum" value="1">
    <!-- input type="hidden" name="goodsCodeTmp" id="goodsCodeTmp"/>
    <input type="hidden" name="goodsNameTmp" id="goodsNameTmp"/>    
    <input type="hidden" name="inDateTmp" id="inDateTmp"/>
    <input type="hidden" name="mawbTmp" id="mawbTmp"/>
    <input type="hidden" name="hawbTmp" id="hawbTmp"/>
    <input type="hidden" name="batchNoTmp" id="batchNoTmp"/>
    <input type="hidden" name="stockQtyTmp" id="stockQtyTmp"/-->
</div>
</body>
</html>